﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using NHibernate.Cfg;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;

namespace LibYanu.DAO
{
    public static class ScInitializer<TMapGen>
        where TMapGen : IPersistenceModelGenerator, new()
    {
        private static ILog log = LogManager.GetLogger(typeof(ScInitializer<TMapGen>));
        static ScInitializer()
        {

            if (Sc<TMapGen>.sessionFactory == null)
            {
                try
                {
                    //Configuration cfg = new Configuration();
                    //cfg.ConfigureUsing("NHibernate.config");
                    //cfg.SetListeners(typeof(TMapGen).Assembly);
                    //Sc<TMapGen>.Configuration = cfg;
                    //Sc<TMapGen>.SetSessionFactory(Fluently.Configure(cfg)
                    //    .Mappings(m => Sc<TMapGen>.MappingInitialization(m))
                    //    .BuildSessionFactory());




                    Sc<TMapGen>.SetSessionFactory(Fluently.Configure()
                        .Database(MsSqlConfiguration.MsSql2008.ConnectionString(string.Format(@"Server={0};initial catalog=ASPNETDB;user={1};password={2};", ".\\Development", "sa", "Password123")).ShowSql())
                        .Mappings(m => Sc<TMapGen>.MappingInitialization(m))
                        .CurrentSessionContext("call")
                        .BuildSessionFactory());
                    
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                    throw ex;
                }
            }
        }

        public static void Init()
        {
            Init(null);
        }

        public static void Init(string diff)
        {
            Sc<TMapGen>.SetDiff(diff);
        }
    }
}
